express = require 'express'
router = express.Router()


router.get '/',(req,res) ->
#  req.api.v4.get {
#    action: 'corpnotify.info.page'
#    sessionkey: req.cookies['__t'] ? 'sessionKey'
#    companyid: 10000
#    pagesize: 10
#    pageindex: 1
#    id: 1
#  }
  res.render 'index'
#  .then (data) ->
#
#  .catch next
router.get '/addNotice',(req,res) ->
  req.api.v4.get {
    action:'corpnotify.info.get'
    id:111
  }
  .then (data)->
    res.render 'addNotice'
  .catch next

router.post '/companypostDelbyids',(req,res,next) ->
  params = req.body
  req.api.v4.post {
    action:'corpnotify.info.delbyids'
    companyid: req.companyId
    sessionkey: req.cookies['__t'] ? 'sessionKey'
    ids: params.ids
  }
  .then (data) ->
    if not data.status
      return M.Promise.reject new M.errors.TipsError data.msg
    res.json {status: true, data: data.data}
  .catch next

router.post '/companypostAdd',(req,res,next) ->
  params = req.body
  req.api.v4.post {
    action:'corpnotify.info.add'
    companyid: req.companyId
    sessionkey: req.cookies['__t'] ? 'sessionKey'
    ids: params.ids
    title: params.title ? ''
    content: params.content ? ''
    sort: params.sort ? 1
    display: params.display ? 1
    istop: params.istop ? 0
    sendtype: params.sendtype ? 1
    notifytype: params.notifytype ? 'all'
    publishtime: params.notifytype ? '2010-01-01 00:00:00'
    endtime: params.endtime ? '2099-01-01 00:00:00'
    orgids: params.orgids
    userids: params.userids
  }
  .then (data) ->
    if not data.status
      return M.Promise.reject new M.errors.TipsError data.msg
    res.json {status: true, data: data.data}
  .catch next

module.exports = router